/**
 * Created by Administrator on 2017/10/30.
 */
var myScroll,
    pullDownEl, pullDownOffset,
    pullUpEl, pullUpOffset,
    generatedCount = 0;
var html='';//生成评论内容
var page=1;
/**
 * 下拉刷新 （自定义实现此方法）
 * myScroll.refresh();		// 数据加载完成后，调用界面更新方法
 */
function pullDownAction () {
    setTimeout(function () {	// <-- Simulate network congestion, remove setTimeout from production!
        var el, li, i;
        el = document.getElementById('thelist');

        //for (i=0; i<3; i++) {
            li = document.createElement('li');

            //li.innerText = 'Generated row ' + (++generatedCount);
            html ='<div><image src="Images/fukuan.png" style="width:40px;border-radius:100%;margin-left:-10px"></image>'+
                '<span style="margin-left:10px">你好你好和你斯蒂芬森</span>：</div><hr/>'+
            '<div class="content"> <div class="images"><image src="Images/fukuan.png" style="width:40px;border-radius:100%;"></image>'+
                '<image src="Images/fukuan.png" style="width:40px;border-radius:100%;"></image>'+
                '<image src="Images/fukuan.png" style="width:40px;border-radius:100%;"></image>'+
                '</div><div class="wenzi">花真好看</div></div><hr/><div>'+
                 '<div ><a class="pinglun"> 评论</a> <a class="dianzhan" style="margin-left: 20px;">点赞：</a></div>'+
                '</div><hr/><!--评论内容--><div><span>昵称：</span><span>评论内容</span></br>'+
            '<span>昵称：</span><span>评论内容</span></br>'+
            '<span>昵称：</span><span>评论内容</span></br></div>';
           // console.log(html);
          //  li.className = 'licss';
            li.style.cssText='height:auto';

            li.innerHTML =html;
            //li.className = 'item';
           //el.insertBefore(li, el.childNodes[0]);
           // $('#thelist li').css('height','auto');
        //}

        myScroll.refresh();		//数据加载完成后，调用界面更新方法   Remember to refresh when contents are loaded (ie: on ajax completion)
    }, 1000);	// <-- Simulate network congestion, remove setTimeout from production!
}

/**
 * 滚动翻页 （自定义实现此方法）
 * myScroll.refresh();		// 数据加载完成后，调用界面更新方法
 */
function pullUpAction (object) {
    setTimeout(function () {	// <-- Simulate network congestion, remove setTimeout from production!
        var el, li, i;
        getdatas(page,object);
        page +=1;
       /* el = document.getElementById('thelist');
        for (i=0; i<3; i++) {
            li = document.createElement('li');
            li.innerHTML = 'Generated row ' + (++generatedCount);
            el.appendChild(li, el.childNodes[0]);
        }
        myScroll.refresh();	*/	// 数据加载完成后，调用界面更新方法 Remember to refresh when contents are loaded (ie: on ajax completion)
    }, 1000);	// <-- Simulate network congestion, remove setTimeout from production!
}
function getdatas(page,object){
    $.ajax({
        url: url + 'getChats',
        dataType: 'json',
        type: 'post',
        async: false,
        data: {'page':page},
        success: function (result) {
            // console.log(result);
            var status = result.status;
            var code = result.code;
            var html = '';

            if (status == 1 && code == '1001') {
                var chats = result.data;
                for(var i=0;i<chats.length;i++){
                    html ='';
                    el = document.getElementById('thelist');//创建一个li对象
                    li = document.createElement('li');
                   /* html +='<div><image src="'+chats[i]['headimgs']+'" style="width:40px;border-radius:100%;margin-left:-10px"></image>'+
                        '<span style="margin-left:10px">'+chats[i]['username']+'</span>：</div><hr/><div class="content"> <div class="images">';*/
                    html +='<div class="item"><div class="c-header clearfix" >'+
                        '						<div class="head fl">'+
                        '							<img src="'+chats[i]['headimgs']+'"/>'+
                        '						</div>'+
                        '						<div class="title fl">'+
                        '							<p>'+chats[i]['username']+'</p>'+
                        '						</div>'+
                        '					</div>'+
                        '					<ul class="img-list clearfix">';
                    var imgs = chats[i]['img'];
                    if(imgs!=''&&imgs.length>0){
                        for(var n=0;n<imgs.length;n++){
                            html +='<image src="'+imgs[n]+'"  style="width:40px;height:70px;"></image>';
                        }
                    }
                    html +='</ul>';
                    var content = chats[i]['content'];
                    html +='<div class="c-content">'+
                        content+
                        '</div>';
                    html +='<div class="c-footer">'+
                        '<a class="comment-btn pinglun" href="javascript:void(0);" onclick="pinglun('+chats[i]['id']+')"></a><span class="good-btn dianzhan'+chats[i]['id']+'" onclick="dianzhan('+chats[i]['id']+')">'+chats[i]['dianzhan']+'</span>'+
                        '</div>'+'<div class="reply-box">';
                   // html +='</div><div class="wenzi">'+content+'</div></div><hr/><div><div><a class="pinglun" onclick="pinglun('+chats[i]['id']+')"> 评论</a> <a class="dianzhan'+chats[i]['id']+'" style="margin-left: 20px;" onclick="dianzhan('+chats[i]['id']+')">点赞：'+chats[i]['dianzhan']+'</a></div></div><hr/><!--评论内容--><div class="pldetails'+chats[i]['id']+'">';

                    var id = chats[i]['id'];
                    id = id+"";
                    var chatdetails = result.chatdetails;
                    var chatdetailsss =chatdetails[id];
                    for(var j=0;j<chatdetailsss.length;j++){
                       // html +='<span>'+chatdetailsss[j]['username']+': </span><span>'+chatdetailsss[j]['content']+'</span></br>';
                        html +='<dl><dd>'+
                            '								<div class="c-header clearfix">'+
                            '									<div class="head fl" style="display: left">'+
                            '										<img src="Images/default.jpg"/>'+
                            '									</div>'+
                            '									<div class="title fl" style="display: left">'+
                            '										<p>'+chatdetailsss[j]['username']+':</p>'+
                            '									</div>'+

                            '								<div class="c-content">&nbsp;&nbsp;'+
                            chatdetailsss[j]['content']+
                            '								</div>'+ '</div>'+
                            '							</dd></dl>';
                    }
                    html += '</div></div>';
                    console.log(html);
                    li.style.cssText='height:auto';
                    li.innerHTML =html;
                    el.appendChild(li, el.childNodes[0]);
                    myScroll.refresh();
                }

            }else{
                object.querySelector('.pullUpLabel').innerHTML = '没有数据了';
            }
        },
        error: function (result) {
            console.log("error")
        }
    });
}
/**
 * 初始化iScroll控件
 */
function loaded() {
    pullDownEl = document.getElementById('pullDown');
    pullDownOffset = pullDownEl.offsetHeight;
    pullUpEl = document.getElementById('pullUp');
    pullUpOffset = pullUpEl.offsetHeight;

    myScroll = new iScroll('wrapper', {
        scrollbarClass: 'myScrollbar', /* 重要样式 */
        useTransition: false, /* 此属性不知用意，本人从true改为false */
        topOffset: pullDownOffset,
        onRefresh: function () {

            if (pullDownEl.className.match('loading')) {
                pullDownEl.className = '';
                pullDownEl.querySelector('.pullDownLabel').innerHTML = '下拉刷新...';
            } else if (pullUpEl.className.match('loading')) {
                pullUpEl.className = '';
                pullUpEl.querySelector('.pullUpLabel').innerHTML = '上拉加载更多...';
            }
        },
        onScrollMove: function () {
            if (this.y > 5 && !pullDownEl.className.match('flip')) {
                pullDownEl.className = 'flip';
                pullDownEl.querySelector('.pullDownLabel').innerHTML = '松手开始更新...';
                this.minScrollY = 0;
            } else if (this.y < 5 && pullDownEl.className.match('flip')) {
                pullDownEl.className = '';
                pullDownEl.querySelector('.pullDownLabel').innerHTML = '下拉刷新...';
                this.minScrollY = -pullDownOffset;
            } else if (this.y < (this.maxScrollY - 5) && !pullUpEl.className.match('flip')) {
                pullUpEl.className = 'flip';
                pullUpEl.querySelector('.pullUpLabel').innerHTML = '松手开始更新...';
                this.maxScrollY = this.maxScrollY;
            } else if (this.y > (this.maxScrollY + 5) && pullUpEl.className.match('flip')) {
                pullUpEl.className = '';
                pullUpEl.querySelector('.pullUpLabel').innerHTML = '上拉加载更多...';
                this.maxScrollY = pullUpOffset;
            }
        },
        onScrollEnd: function () {
            if (pullDownEl.className.match('flip')) {
                pullDownEl.className = 'loading';
                pullDownEl.querySelector('.pullDownLabel').innerHTML = '加载中...';
                pullDownAction();	// Execute custom function (ajax call?)
            } else if (pullUpEl.className.match('flip')) {
                pullUpEl.className = 'loading';
                pullUpEl.querySelector('.pullUpLabel').innerHTML = '加载中...';
                pullUpAction(pullUpEl);	// Execute custom function (ajax call?)
            }
        }
    });
    getdatas(page,pullDownEl); page +=1;
    setTimeout(function () { document.getElementById('wrapper').style.left = '0'; }, 800);
}

//初始化绑定iScroll控件
document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);
document.addEventListener('DOMContentLoaded', loaded, false);

//点赞数量
function dianzhan(id){
    if(sessionStorage.getItem('dianzhantime')!=''&&sessionStorage.getItem('dianzhantime')!=null){
        var date1 = sessionStorage.getItem('dianzhantime');
        var date2=new Date();    //当前时间
        var date3=date2.getTime()-date1; //时间差的毫秒数
        //计算出相差天数
        var days=Math.floor(date3/(24*3600*1000));
        if(days<1){
           alert('同一天中不可重复提交！');
        }
    }
    $.ajax({
        url: url + 'dianzhan',
        dataType: 'json',
        type: 'post',
        async: false,
        data: {'id':id},
        success: function (result) {
            var status = result.status;
            var code = result.code;
            if (status == 1 && code == '1001') {
                var data = result.data;
                var dianzhan = data['dianzhan'];
                $('.dianzhan'+id).html(dianzhan);
                sessionStorage.setItem('dianzhantime',Date.parse(new Date()));

            }else if(status == 1 && code == '1002'){
                alert('同一天不可以重复点赞！');
            }
        },
        error: function (result) {
            console.log("error")
        }
    });
}
